import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'
import pkg from './package.json'

// 获取端口号，默认为 8002
const port = pkg.microApp?.port || 8002

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    port,
    cors: true,
    origin: `http://localhost:${port}`
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    }
  },
  // 确保正确的基础路径
  base: `/${pkg.name}/`,
  // 配置 qiankun 所需的输出格式
  build: {
    target: 'esnext',
    lib: {
      entry: resolve(__dirname, 'src/main.jsx'),
      formats: ['umd'],
      name: pkg.name,
    }
  }
})